package com.ymatou.iapi.trading.operate.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeLogisticsTypeReq;
import com.ymatou.iapi.trading.operate.service.ChangeLogisticsTypeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 更新物流方式 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeLogisticsType {

	private static ChangeLogisticsTypeReq codnbean;
	private static ChangeLogisticsTypeCall codncall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新物流方式");
	}

	@Before
	public void caseUp() {

		codnbean = new ChangeLogisticsTypeReq();
		codncall = new ChangeLogisticsTypeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(codncall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_001() {
		Logger.start(true, "更新物流方式成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.SUCCESS, codncall.getCode(), "验证Code");

			// 数据库验证
			DefaultWapper database = new DefaultWapper();

			Logger.verifyEquals(codnbean.getLogisticsType(),

					(Integer) database.selectOrderInfoByOderId(orderId).get(0)
							.get("iCatalogStatus"), "验证更新为新物流单号");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_002() {
		Logger.start(true, "订单号不存在");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = 222;
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.FAILED201, codncall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.BCODE6, codncall.getBCode(), "验证bcode");

			Logger.verifyEquals(
					String.format("订单号%d不存在", codnbean.getOrderId()),
					codncall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_003() {
		Logger.start(true, "订单号不存在");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = 123;

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.FAILED201, codncall.getCode(), "验证code");
			Logger.verifyEquals("103", codncall.getBCode(), "验证bcode");

			Logger.verifyEquals(String.format("用户%d不是订单%d的所有者",
					codnbean.getSellerId(), codnbean.getOrderId()), codncall
					.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_004() {
		Logger.start(true, "订单号为null");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = 123;

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			//codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals("400", codncall.getCode(), "验证code");
			Logger.verifyEquals("100", codncall.getBCode(), "验证bcode");

			Logger.verifyEquals("orderId:orderId未提供", codncall.getMsg(),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_005() {
		Logger.start(true, "订单为下单,修改物流方式失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService.acceptOrder();

			int orderId = 222;
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.BondedDelivery
					.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.FAILED201, codncall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.BCODE6, codncall.getBCode(), "验证bcode");

			Logger.verifyEquals(
					String.format("订单号%d不存在", codnbean.getOrderId()),
					codncall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_006() {
		Logger.start(true, "更新物流方式修改前由 需要上传身份证-->不用上传身份证");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderService.logisticsType = LogisticsTypeEnum.SailProtect;

			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.US.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.SUCCESS, codncall.getCode(), "验证Code");

			// 数据库验证
			DefaultWapper database = new DefaultWapper();

			Logger.verifyEquals(codnbean.getLogisticsType(),

					(Integer) database.selectOrderInfoByOderId(orderId).get(0)
							.get("iCatalogStatus"), "验证更新为新物流单号");

			Thread.sleep(3000);

			Logger.verifyEquals(false, database.selectOrderExtByOderId(orderId)
					.get(0).get("bIsNeedUploadIdCard"), "需要上传身份证信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_007() {
		Logger.start(true, "更新物流方式修改前由 不需要上传身份证-->需要上传身份证");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderService.logisticsType = LogisticsTypeEnum.US;

			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.SailProtect.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.SUCCESS, codncall.getCode(), "验证Code");

			// 数据库验证
			DefaultWapper database = new DefaultWapper();

			Logger.verifyEquals(codnbean.getLogisticsType(),

					(Integer) database.selectOrderInfoByOderId(orderId).get(0)
							.get("iCatalogStatus"), "验证更新为新物流单号");

			Thread.sleep(3000);

			Logger.verifyEquals(true, database.selectOrderExtByOderId(orderId)
					.get(0).get("bIsNeedUploadIdCard"), "需要上传身份证信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_008() {
		Logger.start(true, "更新物流方式修改前由 不需要上传身份证-->不需要上传身份证");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderService.logisticsType = LogisticsTypeEnum.US;

			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.US.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.SUCCESS, codncall.getCode(), "验证Code");

			// 数据库验证
			DefaultWapper database = new DefaultWapper();

			Logger.verifyEquals(codnbean.getLogisticsType(),

					(Integer) database.selectOrderInfoByOderId(orderId).get(0)
							.get("iCatalogStatus"), "验证更新为新物流单号");

			Thread.sleep(3000);

			Logger.verifyEquals(false, database.selectOrderExtByOderId(orderId)
					.get(0).get("bIsNeedUploadIdCard"), "需要上传身份证信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeLogisticsType_009() {
		Logger.start(true, "更新物流方式修改前由 需要上传身份证-->需要上传身份证");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderService.logisticsType = LogisticsTypeEnum.SailProtect;

			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			codnbean.setLogisticsType(LogisticsTypeEnum.SailProtect.getCode());

			codnbean.setOrderId(orderId);
			codnbean.setSellerId(sellerId);

			// 发送请求
			codncall.setData(codnbean);
			codncall.callService();

			Logger.verifyEquals(ECode.SUCCESS, codncall.getCode(), "验证Code");

			// 数据库验证
			DefaultWapper database = new DefaultWapper();

			Logger.verifyEquals(codnbean.getLogisticsType(),

					(Integer) database.selectOrderInfoByOderId(orderId).get(0)
							.get("iCatalogStatus"), "验证更新为新物流单号");

			Thread.sleep(3000);

			Logger.verifyEquals(true, database.selectOrderExtByOderId(orderId)
					.get(0).get("bIsNeedUploadIdCard"), "需要上传身份证信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}